package com.zcx.qshop.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import com.zcx.helper.bound.BoundView;
import com.zcx.helper.util.UtilToast;
import com.zcx.qshop.R;
import com.zcx.qshop.QSApplication;
import com.zcx.qshop.action.LoginAction;
import com.zcx.qshop.util.UtilRegular;

/**
 * Created by Administrator on 2015/8/9 0009.
 */
public class LoginActivity extends QSActivity implements View.OnClickListener{

    @BoundView(R.id.login_back) private View back;

    @BoundView(R.id.login_get_back) private View getBack;

    @BoundView(R.id.login_register) private View register;

    @BoundView(R.id.login_username) private EditText username;

    @BoundView(R.id.login_password) private EditText password;

    @BoundView(R.id.login_login) private View login;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_login);

        back.setOnClickListener(this);

        getBack.setOnClickListener(this);

        register.setOnClickListener(this);

        login.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {

        switch (v.getId()){

            case R.id.login_back:

                finish();

                break;

            case R.id.login_login:

                String urn = username.getText().toString(), psd = password.getText().toString();

                if(urn.equals("")){

                    UtilToast.show(this, "请输入手机号");

                    return;

                }else if(!UtilRegular.mobile(urn)){

                    UtilToast.show(this, "手机格式错误");

                    return;

                }else if(psd.equals("")){

                    UtilToast.show(this, "请输入密码");

                    return;

                }else{

                    QSApplication.LoginAction.login(this, urn, psd, new LoginAction.OnLoginListener() {

                        @Override
                        public void onSuccess(String uid) {

                            finish();

                            UtilToast.show(LoginActivity.this, "登录成功");

                            try {

                                startActivity(new Intent(LoginActivity.this, (Class<?>) getIntent().getExtras().get("Class")));

                            } catch (Exception e) {}

                            if (CommodityDetailActivity.onCommodityDetailChangeListener != null) {

                                CommodityDetailActivity.onCommodityDetailChangeListener.onCommodityDetailChange();

                            }

                        }

                        @Override
                        public void onFail() {

                            UtilToast.show(LoginActivity.this, "登录失败");

                        }

                    });

                }

                break;

            case R.id.login_get_back:

                startVerifyActivity(GetBackActivity.class);

                break;

            case R.id.login_register:

                startVerifyActivity(RegisterActivity.class);

                break;

        }

    }

}
